<template>
    <div>
        <el-table
            :data="tableData"
            :header-cell-style="{
                background: 'rgb(255, 191, 191)',
                color: 'rgb(44,44,44)'
            }"
            stripe
            v-loading="tableLoading"
        >
            <el-table-column prop="name" label="活动名称" align="center"></el-table-column>
            <el-table-column prop="startTime" label="开始时间" align="center"></el-table-column>
            <el-table-column prop="endTime" label="结束时间" align="center"></el-table-column>
            <el-table-column prop="sort" label="排序" align="center" width="100"></el-table-column>
            <el-table-column prop="state" label="活动状态" align="center">
                <template slot-scope="scope">
                    <span>{{ ['未编辑', '未发布', '已发布', '待审批', '已驳回'][scope.row.state] }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" @click="onOpen(scope.row)">查看</el-button>
                    <el-button v-if="scope.row.state == 3 && permissions.cp_activity_approve" type="text" @click="activitySwitch(scope.row)">审批</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            class="madp-pagination"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page.current"
            :page-sizes="[10, 20, 30, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
        ></el-pagination>
        <el-dialog title="活动审批" :visible.sync="dialogShow" width="800px" :close-on-click-modal="false">
            <el-form label-width="130px" :model="form" ref="form" :rules="rules">
                <el-form-item label="活动名称">
                    {{ form.name }}
                </el-form-item>
                <el-form-item label="活动时间"> {{ form.startTime }} - {{ form.endTime }} </el-form-item>
                <el-form-item label="审批意见" prop="opinion">
                    <el-input v-model="form.opinion" type="textarea" key="opinion" maxlength="500" :autosize="{ minRows: 3 }" show-word-limit></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="dialogHandleSubmit(0)">拒绝</el-button>
                <el-button type="primary" @click="dialogHandleSubmit(1)">同意</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import storeApi from '@/api/store/store.js';
import approveApi from '@/api/store/approve.js';
import { mapGetters } from 'vuex';
import approveDialog from '@/components/product-approve/index';
export default {
    components: { approveDialog },
    props: {
        searchT: {
            type: String,
            default: ''
        },
        tableType: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            // 表格数据
            tableData: [],
            // 分页
            page: {
                current: 1,
                size: 10,
                total: 0
            },
            // loading
            tableLoading: false,
            // 活动信息
            form: {},
            // 弹窗开关
            dialogShow: false,
            // 校验规则
            rules: {
                opinion: [
                    {
                        required: true,
                        message: '请输入审批意见',
                        trigger: 'blur'
                    }
                ]
            }
        };
    },
    computed: {
        ...mapGetters({
            permissions: 'permissions'
        })
    },
    created() {
        this.getList();
    },
    methods: {
        // 点击审批
        activitySwitch(data) {
            this.dialogShow = true;
            this.form = { ...data, opinion: '' };
        },
        // 分页
        handleSizeChange(val) {
            this.page.size = val;
            this.getList(1);
        },
        handleCurrentChange(val) {
            this.page.current = val;
            this.getList(1);
        },
        // 查询
        getList(data) {
            if (!data) this.page.current = 1;
            this.tableLoading = true;
            approveApi
                .productList({
                    name: this.searchT,
                    type: 2,
                    state: this.tableType,
                    current: this.page.current,
                    size: this.page.size
                })
                .then((res) => {
                    this.tableData = res.data.data.records;
                    if (this.tableData.length == 0 && this.page.current > 1) {
                        this.page.current--;
                        this.getList(1);
                        return;
                    }
                    this.page.total = res.data.data.total;
                    this.tableLoading = false;
                })
                .catch(() => {
                    this.tableLoading = false;
                });
        },
        // 点击查看
        onOpen(info) {
            storeApi
                .productInfo({
                    prodId: info.id
                })
                .then((res) => {
                    let data = res.data.data;
                    this.$emit('handleView', data);
                });
        },
        // 审批
        dialogHandleSubmit(val) {
            this.$confirm('是否确认' + ['拒绝', '同意'][val] + '通过审批', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$refs.form.validate((valid) => {
                        if (valid) {
                            let { id, opinion } = this.form;
                            approveApi.productApprove({ id, opinion, state: val + 1 }).then((res) => {
                                if (res.data.code != 0) return;
                                this.$message({
                                    type: 'success',
                                    message: '审批成功!'
                                });
                                this.dialogShow = false;
                                this.$emit('handleUpdate');
                            });
                        }
                    });
                })
                .catch(() => {});
        }
    }
};
</script>
